<template>
    <div>
        <navtitle title="登录页" />
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" left-icon="phone" placeholder="&emsp;手机号"
                    :rules="[{ pattern, message: '请输入正确内容' }]" />
                <van-field v-model="password" type="password" name="password" left-icon="lock"
                    placeholder="&emsp;6位及以上字母数字结合" right-icon="closed-eye"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
                <van-button round block native-type="submit" style="margin-top: 15px;" @click="toreg">
                    去注册
                </van-button>
            </div>
        </van-form>
    </div>
</template>

<script setup>
import navtitle from '@/components/navtitle.vue';
import { useRoute, useRouter } from 'vue-router'

const router = useRouter()
import { ref } from 'vue'
const username = ref('');
const password = ref('');
//正则校验
const pattern = /^1[3-9]\d{9}$/
// 引入登录接口
import { loginApi } from '@/api/api';
import { showFailToast, showSuccessToast } from 'vant';
const onSubmit=()=>{
    loginApi(username.value,password.value).then((res)=>{
        console.log('登录',res);
        if(res.data.code==0){
            showSuccessToast('登录成功')
            router.push('/home/index')
            localStorage.setItem('token',res.data.data.token)
        }else{
            showFailToast('登录失败')
            username.value='',
            password.value=''
        }
    })
}

const toreg=()=>{
    router.push('/regies')
}
</script>

<style lang="scss" scoped></style>